;(function () {
	new Vue({
		el: '#building',
		data: {
			quanxian:JSON.parse(localStorage.getItem('permissions')),
			projectId: hlUrlUtil.GetQueryString('projectId'),
			groupId: null,
			groupName:null,
			groupPid:null,
			groupPcode:null,
			groupPnode:null,
			groupBuildingsList:[],
			groupBuildingIds:[],
			groupLevel:null,
			groupIndex:null,
			buildingsList: [],
			buildingGroupOneData:[],
			buildingGroupTwoData:[],
			buildingGroupThreeData:[],
			isShowGroupOne:false,
			isShowGroupTwo:false,
			isShowGroupThree:false,
			isShowAddGroupBtn:false,
			selectedGroupOneData:[],
			selectedGroupTwoData:[],
			selectedGroupThreeData:[],
			addGroupPopoverData:[{'name':'新增同级组合','code':'A'},{'name':'新增下级组合','code':'B'}],
			addGroupPopoverPosition:{'left':1000000,'top':1000000},
			addGroupPopoverPositionCache:{'left':1000000,'top':1000000},
			addGroupLevel:null,
			addEditTitle: '新增',
			addEditFlag: false,
			addEditValue: '',
			addEditImg: '',
			buildingId: '',
			startNum: '',
			endNum: '',
			debarNum: '',
			showBuildingConfigIndex: null,
			delFlag: false,
			floorList: [],
			addFloors: [],
			checkAll: false,
			floorIds: [],
			picFloors: [],
			picFloorIds: [],
			picFloorFlag: false,
			picUrl: '',
			isShowGroupDialog:false,
			isSelectedGroup:false,
			isAddFlag:false,
			isEditFlag:false,
			isDelFlag:false,
			groupTitle:null,
		},
		created(){
			this.getGroupBuildingData()
			this.getBuildingGroupData().then( data => {
				this.buildingGroupOneData = data
				if(this.buildingGroupOneData.length != 0){
					this.isShowGroupOne = true
				}else{
					this.isShowAddGroupBtn = true
				}

			})

			// 初始化权限  c330 组合的增删改权限
			if(this.quanxian.indexOf('c330') > -1){
				this.isAddFlag = true
				this.isEditFlag = true
				this.isDelFlag = true
			}
		},
		methods: {
			// 获取楼宇组合下的楼宇
			getGroupBuildingData(){
				var self = this

				self.groupBuildingIds = []

				return new Promise(resolve => {
					hlhttp.get({
						url: 'project/grouping/_building',
						data: { project_id: self.projectId , grouping_id: self.groupId || '#' },
						success: (data) => {
							// configState == ‘Y’ 已配置的选中
							self.buildingsList = hlArrayUtil.splitInToTwoimensionalArray(data,4)
							self.groupBuildingsList = hlArrayUtil.replaceData(data, [{key: 'buildingId', rKey: 'code'}]);
							self.groupBuildingsList.forEach(el => {
								if(el.configState == 'Y') self.groupBuildingIds.push(el.code)
							})
							resolve(data)
						}
					})
				})

			},
			validateGroup(){
				if(!this.groupName){
					hl.toast('组合名称不能为空')
					return false
				}

				return true
			},
			// 新增组合
			addBuildingGroup(){
				const self = this,valid = this.validateGroup()

				if(!valid) return false
				hlhttp.post({
					url: 'project/grouping_info',
					data: {
						project_id: this.projectId ,
						name: this.groupName || '#' ,
						pid: this.groupPid || '#' ,
						building_ids:this.groupBuildingIds.length ? this.groupBuildingIds.join() : '#'
					},
					success: (data) => {
						hl.toast('新增组合成功')

						if(self.groupLevel == 1 && self.addGroupLevel == '同级'){
							// self.buildingGroupOneData.push({'name':self.groupName , 'id':data.id , 'pid':self.groupPid})
							self.isShowGroupOne = true
							self.isShowAddGroupBtn = false
							self.groupId = self.groupPid
							self.getBuildingGroupData().then(data => { this.buildingGroupOneData = data })
						}
						if(self.groupLevel == 2 && self.addGroupLevel == '同级'){
							// self.buildingGroupTwoData.push({'name':self.groupName , 'id':data.id , 'pid':self.groupPid})
							self.isShowGroupTwo = true
							self.groupId = self.groupPid
							self.getBuildingGroupData().then(data => { this.buildingGroupTwoData = data })
						}
						if(self.groupLevel == 3 && self.addGroupLevel == '同级'){
							 // self.buildingGroupThreeData.push({'name':self.groupName , 'id':data.id , 'pid':self.groupPid})
							 self.isShowGroupThree = true
							 self.groupId = self.groupPid
							 self.getBuildingGroupData().then(data => { this.buildingGroupThreeData = data })
						}
						if(self.groupLevel == 1 && self.addGroupLevel == '下级'){
							self.isShowGroupTwo = true
						}
						if(self.groupLevel == 2 && self.addGroupLevel == '下级'){
							self.isShowGroupThree = true
						}

						self.isShowGroupDialog = false
						self.groupName = null
					}
				})
			},
			// 编辑组合
		 	editBuildingGroup(){
				var self = this,valid = this.validateGroup()

				if(!valid) return false
				hlhttp.put({
					url: 'project/grouping_info',
					data: {
						grouping_id: self.groupId,
						name: self.groupName || '#' ,
						building_ids:self.groupBuildingIds.length ? self.groupBuildingIds.join() : '#'
					},
					success: (data) => {
						hl.toast('编辑组合成功')
						// 根据当前组合 对应更新数据
						if(self.groupLevel == 1){
							self.$set(self.buildingGroupOneData[self.groupIndex],'name',self.groupName)
							self.selectedGroupOneData.reverse()
						}
						if(self.groupLevel == 2){
							self.$set(self.buildingGroupTwoData[self.groupIndex],'name',self.groupName)
							self.selectedGroupTwoData.reverse()
						}
						if(self.groupLevel == 3){
							self.$set(self.buildingGroupThreeData[self.groupIndex],'name',self.groupName)
							self.selectedGroupThreeData.reverse()
						}

						self.isShowGroupDialog = false
						self.groupName = null
					}
				})
			},
			// 获取楼宇组合
			getBuildingGroupData(){
				return new Promise(resolve => {
					hlhttp.get({
						url: 'project/grouping_info/_subclass',
						data: { project_id: this.projectId , grouping_id: this.groupId || '#' },
						success: (data) => {
							data = data.filter( el => el.dataLevel == 'C' )
							const dealData = hlArrayUtil.replaceData(data, [{key: 'id', rKey: 'code'}]);
							resolve(dealData)
						}
					})
				})

			},
			getOneGroupNode(data){
				this.groupId = data[0].id
				this.isSelectedGroup = true

				this.getGroupBuildingData()
				this.getBuildingGroupData()
						.then(data => {
							this.buildingGroupTwoData = data
							this.selectedGroupTwoData = []
							this.selectedGroupThreeData = []

							if(this.buildingGroupTwoData.length == 0){
								this.isShowGroupTwo = false
								this.isShowGroupThree = false
							}else{
								this.isShowGroupTwo = true
							}
						})
			},
			getTwoGroupNode(data){
				this.groupId = data[0].id
				this.isSelectedGroup = true

				this.getGroupBuildingData()
				this.getBuildingGroupData()
						.then(data => {
							this.buildingGroupThreeData = data
							this.selectedGroupThreeData = []
							if(this.buildingGroupThreeData.length == 0){
								this.isShowGroupThree = false
							}else{
								this.isShowGroupThree = true
							}
						})
			},
			getThreeGroupNode(data){
				this.groupId = data[0].id
				this.isSelectedGroup = true
				this.getGroupBuildingData()

			},
			addInitGroup(){
				this.isShowGroupDialog = true
				this.groupName = null
				this.groupBuildingIds = []
				this.groupTitle = '新增组合'
				this.addGroupLevel = '同级'
				this.groupLevel = 1
			},
			addGroup(level,arguments){
				const data = arguments[0],event = arguments[2]

				this.addGroupPopoverPosition.left = event.clientX - event.offsetX
				this.addGroupPopoverPosition.top = event.clientY - event.offsetY
				this.addGroupPopoverPositionCache.left = this.addGroupPopoverPosition.left
				this.addGroupPopoverPositionCache.top = this.addGroupPopoverPosition.top
				this.groupId = data.id
				this.groupPid = data.pid
				this.groupLevel = level
			},
			leaveGroup(){
				this.addGroupPopoverPosition.left = 1000000
				this.addGroupPopoverPosition.top = 1000000
			},
			enterPopover(){
				this.addGroupPopoverPosition.left = this.addGroupPopoverPositionCache.left
				this.addGroupPopoverPosition.top = this.addGroupPopoverPositionCache.top
			},
			leavePopover(){
				this.leaveGroup()
			},
			editGroup(level,arguments){
				const data = arguments[0],index = arguments[1]

				this.groupTitle = '编辑组合'
				this.groupLevel = level
				this.groupIndex = index
				this.groupId = data.id
				this.groupName = data.name
				this.getGroupBuildingData()
				.then(() => this.isShowGroupDialog = true )
			},
			delGroup(level,arguments){
				const data = arguments[0],index = arguments[1],self = this

				self.groupId = data.id

				hllayer.confirm('确认要删除该组合吗？',function(){
					hlhttp.delete({
						url: 'project/grouping_info?project_id=' + self.projectId + '&grouping_id=' + self.groupId,
						success: (data) => {
							hl.toast('删除成功')
							// 如果删除项是选中项，则初始化 ； 如果不是选中项，则直接删除
							if(level == 1){
								if(self.groupId == self.selectedGroupOneData[0]){
									self.selectedGroupOneData = []
								}

								self.buildingGroupOneData.splice(index,1)
								self.buildingGroupTwoData = []
								self.buildingGroupThreeData = []
								self.selectedGroupTwoData = []
								self.selectedGroupThreeData = []
								if(self.buildingGroupOneData.length == 0){
									self.isShowGroupOne = false
									self.isShowGroupTwo = false
									self.isShowGroupThree = false
									self.isShowAddGroupBtn = true
								}

								return false
							}

							if(level == 2){
								if(self.groupId == self.selectedGroupTwoData[0]){
									self.selectedGroupTwoData = []
								}

								self.buildingGroupTwoData.splice(index,1)
								self.buildingGroupThreeData = []
								self.selectedGroupThreeData = []
								if(self.buildingGroupTwoData.length == 0){
									self.isShowGroupTwo = false
									self.isShowGroupThree = false
								}

								return false
							}

							if(level == 3){
								if(self.groupId == self.selectedGroupThreeData[0]){
									self.selectedGroupThreeData = []
								}
								self.buildingGroupThreeData.splice(index,1)
								if(self.buildingGroupThreeData.length == 0) self.isShowGroupThree = false

								return false
							}
						}
					})
				})

			},
			selectedGroupBuilding(){

			},
			confirmUpdateGroup(){
				if(this.groupTitle == '新增组合') this.addBuildingGroup()
				if(this.groupTitle == '编辑组合') this.editBuildingGroup()
			},
			cancelGroup(){
				this.isShowGroupDialog = false
			},
			resetGroup(){
				const self = this

				self.buildingGroupTwoData = []
				self.buildingGroupThreeData = []
				self.selectedGroupOneData = []
				self.selectedGroupTwoData = []
				self.selectedGroupThreeData = []
				self.isSelectedGroup = false
			},
			confirmAddGroup(data){
				const code = data[0]['code']

				const addPeerGroup = () => { this.addGroupLevel = '同级' }

				const addNextGroup = () => {
					this.groupPid = this.groupId
					this.addGroupLevel = '下级'
				}

				if(code == 'A') addPeerGroup()
				if(code == 'B') addNextGroup()

				this.groupName = null
				this.groupBuildingIds = []
				this.groupTitle = '新增组合'
				this.isShowGroupDialog = true
			},
			//楼宇新增key
			buildingKey () {
				this.addEditImg = '';
				this.addEditValue = '';
				this.addEditFlag = false;
			},
			//新增显示
			addShow () {
				this.addEditFlag = true;
				this.showBuildingConfigIndex = null;
				this.buildingId = null;
				this.addEditTitle = '新增';
			},
			//上传图片
			buildingImgUp () {
				document.querySelector('.image-up').click()
			},
			//新增楼宇
			addBuilding () {
				hlhttp.post({
					url: 'building',
					data: {
						names: this.addEditValue,
						previews: this.addEditImg,
						project_id: this.projectId
					},
					success: () => {
						this.getGroupBuildingData()
						hl.toast('操作成功')
						this.buildingKey()
					}
				})
			},
			//删除楼宇
			delBuilding (data) {
				hllayer.confirm('是否确认删除该楼宇？', () => {
					hlhttp.delete({
						url: 'building?project_id='+this.projectId+'&building_id='+data.buildingId,
						success: () => {
							hl.toast('操作成功')
							this.getGroupBuildingData()
						}
					})
				})
			},
			//编辑显示
			editShow (data) {
				this.addEditFlag = true;
				this.addEditTitle = '编辑';
				this.addEditValue = data.name;
				this.addEditImg = data.preview;
				this.buildingId = data.buildingId;
			},
			//编辑楼宇
			editBuilding () {
				hlhttp.put({
					url: 'building',
					data: {
						names: this.addEditValue,
						previews: this.addEditImg,
						project_id: this.projectId,
						building_id: this.buildingId
					},
					success: () => {
						this.getGroupBuildingData()
						hl.toast('操作成功')
						this.buildingKey()
					}
				})
			},
			//配置
			config (i, data) {
				if (data.buildingId == this.buildingId) {
					this.showBuildingConfigIndex = null;
					this.buildingId = null;
					return
				}
				this.delFlag = false;
				this.showBuildingConfigIndex = i;
				this.buildingId = data.buildingId;
				this.floorData()
			},
			//获取楼院下面楼层
			floorData () {
				hlhttp.get({
					url: 'build/floors',
					data: {building_id: this.buildingId, page: 1, limit: 10000},
					success: (data) => {
						data.floors.map(item => {
							item.flag = false;
							item.floorNameFlag = false;
						})
						this.floorList = data.floors;
					}
				})
			},
			//验证楼层是否为数字
			floorValidate (i) {
				this.floorList.map(item => {
					if (item.floor == this.addFloors[i].floor) {
						this.addFloors[i].floor = '';
						hl.toast('楼层已存在')
					}
				})
				if (this.addFloors[i].floor == '') return;
				if (this.addFloors[i].floor == '0') return;
				if (isNaN(this.addFloors[i].floor)) {
					this.addFloors[i].floor = '';
					hl.toast('楼层不是数字，请重新填写')
				}
			},
			//排除楼层验证
			debarFloorValidate () {
				let data = this.debarNum.split(' ');
				let ky = false;
				data.map(item => {
					if (isNaN(item)) {
						ky = true;
					}
				})
				if (ky) {
					hl.toast('楼层只能为数字加空格分割')
					this.debarNum = ''
				}
			},
			//删除添加的楼层
			delFloor (i) {
				this.addFloors.splice(i, 1)
			},
			//生成单个楼层
			addFloor () {
				this.addFloors.push({floorName: '', floor: ''})
			},
			//生成多个楼层
			addMoreFloors () {
				let data = [];
				let floors = [];
				let flag = false;
				this.addFloors = [];
				this.debarNum.split(' ').map(item => {
					floors.push(Number(item))
				})
				this.floorList.map(item => {
					floors.push(item.floor)
				})
				for (let i = this.startNum; i <= this.endNum; i++) {
					data.push({floorName: '', floor: Number(i)})
				}
				data.map(item => {
					if (floors.indexOf(item.floor) == -1) {
						this.addFloors.push(item)
					}
					if (floors.indexOf(item.floor) > -1) {
						flag = true
					}
				})
				if (flag) {
					hl.toast('已为你跳过重复楼层和排除楼层')
				}
				this.startNum = null;
				this.endNum = null;
			},
			//保存单个楼层
			saveFloor (i) {
				if (!this.addFloors[i].floorName) {
					hl.toast('请完善楼层信息')
					return
				}
				if (!this.addFloors[i].floor) {
					hl.toast('请完善楼层信息')
					return
				}
				let data = [{floor_name: this.addFloors[i].floorName, floor: this.addFloors[i].floor + ''}]
				hlhttp.post({
					url: 'building/floor',
					data: {
						project_id: this.projectId,
						building_id: this.buildingId,
						floors: JSON.stringify(data)
					},
					success: () => {
						hl.toast('新增楼层成功')
						this.addFloors.splice(i, 1)
						this.floorData()
					}
				})
			},
			//显示楼层输入框
			showEditFloor (i) {
				this.floorList[i].flag = true;
			},
			//显示楼层输入框
			showEditFloorName (i) {
				this.floorList[i].floorNameFlag = true;
			},
			//编辑楼层
			editFloor (i) {
				hlhttp.put({
					url: 'building/floor',
					data: {
						floor_id: this.floorList[i].floorId,
						project_id: this.floorList[i].projectId,
						building_id: this.floorList[i].buildingId,
						floor_name: this.floorList[i].floorName,
						floor: this.floorList[i].floor,
						previews: '#'
					},
					success: () => {
						hl.toast('操作成功')
						this.floorData()
					}
				})
			},
			//清空新增楼层
			removeAllFloor () {
				hllayer.confirm('此操作不可逆，确定清空楼层数据吗？', () => {
					this.addFloors = [];
					this.startNum = null;
					this.endNum = null;
				})
			},
			//显示删除按钮
			showDel () {
				this.delFlag = true;
			},
			//全选/反全选
			checkAllEvent () {
				this.floorIds = [];
				this.checkAll = !this.checkAll;
				if (this.checkAll) {
					this.floorList.map(item => {
						this.floorIds.push(item.floorId)
					})
				}
			},
			//单选
			checkEvent (data) {
				this.checkAll = false;
				if (this.floorIds.indexOf(data.floorId) > -1) {
					this.floorIds.splice(this.floorIds.indexOf(data.floorId), 1)
					return
				}
				this.floorIds.push(data.floorId)
			},
			// 确认删除
			queryDel () {
				hlhttp.delete({
					url: 'building/floor?floor_ids='+this.floorIds.toString(),
					success: () => {
						this.floorIds = [];
						this.checkAll = false;
						this.delFlag = false;
						hl.toast('操作成功')
						this.floorData()
					}
				})
			},
			//取消
			cancelDel () {
				this.floorIds = [];
				this.checkAll = false;
				this.delFlag = false;
			},
			//显示上传平面图
			showPic () {
				this.picFloorFlag = !this.picFloorFlag;
				if (this.picFloorFlag) {
					this.picFloorsData()
				}
			},
			//获取平面图楼层
			picFloorsData () {
				hlhttp.get({
					url: 'building/floors',
					data: {project_id: this.projectId, building_id: this.buildingId},
					success: (data) => {
						this.picFloors = data;
					}
				})
			},
			// 选择要上传图片的楼层
			selectFloorPic (data) {
				if (this.picFloorIds.indexOf(data.floorId) > -1) {
					this.picFloorIds.splice(this.picFloorIds.indexOf(data.floorId), 1)
					return
				}
				this.picFloorIds.push(data.floorId)
			},
			//关闭上传【平面图
			closePic () {
				this.picUrl = '';
				this.picFloorIds = [];
				this.picFloorFlag = false;
			},
			//保存上传的平面图
			savePic () {
				if (!this.picUrl) {
					hl.toast('请上传平面图')
					return
				}
				if (!this.picFloorIds.length) {
					hl.toast('请选择楼层')
					return
				}
				hlhttp.put({
					url: 'floor/floors/plain_graph',
					data: {
						floor_ids: this.picFloorIds.toString(),
						previews: this.picUrl
					},
					success: () => {
						this.closePic()
						this.floorData()
					}
				})
			},
			//删除平面图
			delPicFloor (data) {
				hllayer.confirm('此操作不可逆，确定清空楼层数据吗？', () => {
					hlhttp.delete({
						url: 'floors/plain_graph?floor_id='+data.floorId,
						success: () => {
							this.floorData()
						}
					})
				})
			}
		},
		mounted () {
			imgupload0('.image-up', (data) => {
				if (this.picFloorFlag) {
					this.picUrl = data.json.url;
					return
				}
				this.addEditImg = data.json.url
			})

		}
	})
} ())
